package rpg.Utils
{
	import flash.filters.BitmapFilterQuality;
	import flash.filters.GlowFilter;
	import flash.text.StyleSheet;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.utils.Dictionary;

	public class TextStyleUtil
	{
		//0代表灰色 1代表白色，2代表蓝色，3代表金黄色，4代表紫色，5代表绿色
		public static var goodsQualityColor : Array = [0xc0c0c0 , 0xffffff , 0x0000ff , 0xffff00 , 0xff00ff , 0x00ff00];

		public static var goodsQuelityHtmlColor : Array = ["#c0c0c0" , "#ffffff" , "#0000ff" , "#ffff00" , "#ff00ff" , "#00ff00"];

		public static var styleSheet : StyleSheet = new StyleSheet();
		
		//内发光滤镜
		private static var insideGlowFilterDic:Dictionary;
		/**
		 * 图标获取内发光滤镜
		 * @param color
		 * @return 
		 */
		public static function getInsideGlowFilter(color:uint):GlowFilter
		{
			if(insideGlowFilterDic == null)
				insideGlowFilterDic = new Dictionary();
			if(insideGlowFilterDic[color])
			{
				return insideGlowFilterDic[color] as GlowFilter;
			}
			insideGlowFilterDic[color] = new GlowFilter(color,1,7,7,1.8,BitmapFilterQuality.HIGH,true);
			return insideGlowFilterDic[color] as GlowFilter;
		}
		
		

		//外发光滤镜
		/**
		 * 白光滤镜
		 */
		public static var whiteFilters : Array = [new GlowFilter(0x999999 , 1 , 2 , 2 , 255)];
		/**
		 * 深绿光滤镜
		 */
		public static var darkGreenFilters : Array = [new GlowFilter(0x0d2421 , 1 , 2 , 2 , 255)];
		/**
		 * 黑色光滤镜
		 */
		public static var blackFilters : Array = [new GlowFilter(0x000000 , 1 , 2 , 2 , 255)];

		/**
		 * 默认字体滤镜  默认所有TextField 都会应用
		 */
		public static var filters : Array;

		public static var defaultTextFormat : TextFormat = new TextFormat("" , 12 , 0xffffff , null , null , null , null , null , "center");


		/**
		 * 默认标题滤镜  默认所有TextField 都会应用
		 */
		public static var titleFilters : Array;

		/**
		 * 默认字体颜色
		 */
		public static var color : uint;
		/**
		 * 默认 Css样式
		 */
		public static var defaultCss : String;


		/**
		 * 正常
		 */
		public static var style_0 : Object;
		/**
		 * 鼠标经过
		 */
		public static var style_1 : Object;

		/**
		 * 按下
		 */
		public static var style_2 : Object;
		/**
		 * 点击
		 */
		public static var style_3 : Object;

		/**
		 * 无效
		 */
		public static var style_4 : Object;


		public static var style_name : Object;
		public static var style_blue : Object;
		public static var style_green : Object;
		public static var style_high : Object;
		public static var style_low : Object;
		public static var style_normal : Object;
		public static var style_warm : Object;


		public function TextStyleUtil()
		{
		}

		public static function getTextField(style : int = 0) : TextField
		{
			var t : TextField = new TextField();
			t.mouseEnabled = false;
			return t;
		}

		/**
		 *
		 * @param st
		 * @param _color
		 * @param size
		 * @param align center left right
		 * @param cssName
		 * @param style 样式  u 下划线  b 粗体 i 斜体
		 * @return
		 *
		 */
		public static function converToHtml(st : String , _color : uint = 10 , size : int = 12 , align : String = "center" , cssName : String = "" , style : String = "") : String
		{

			if (_color == 10)
			{
				_color = color;
			}
			var t : String = '<font color="#' + _color.toString(16) + '" ' + 'size="' + size + '"' + '>' + st + '</font>';
			if (cssName)
			{
				t = '<p align ="' + align + '">' + t + '</p>';

			}
			else
			{
				t = '<p ' + 'class ="' + cssName + '" ' + 'align ="' + align + '">' + t + '</p>';

			}
			switch (style)
			{
				case "i":
					t = '<i>' + t + '</i>';
					break;
				case "u":
					t = '<u>' + t + '</u>';
					break;
				case "b":
					t = '<b>' + t + '</b>';
					break;

			}

			return t;
		}

		//	/(?<=<).*?(?=>)/g)
		/**
		 *
		 * @param txt
		 *
		 */
		public static function getStyleObjectByTextField(txt : TextField) : Object
		{
			var ob : Object = new Object();
			var st : String = txt.htmlText;

			var params : Array = ["color" , "size" , "face" , "class" , "align" , "letterspacing"];
			var len : int = params.length;
			var arr : Array;
			var i : int;
			for (i = 0 ; i < len ; i++)
			{
				var reg : RegExp = new RegExp('(?<=' + params[i] + '=").*?(?=")' , "ig");
				arr = st.match(reg);
				if (arr.length != 0)
				{
					ob[params[i]] = arr[0];
				}
			}
			arr = st.match(/(?<=<).*?(?=>)/g);

			len = arr.length;
			for (i = 0 ; i < len ; i++)
			{
				switch (arr[i])
				{
					case "i":
						ob.style = arr[i];
						break;
					case "u":
						ob.style = arr[i];
						break;
					case "b":
						ob.style = arr[i];
						break;

					case "I":
						ob.style = arr[i];
						break;
					case "U":
						ob.style = arr[i];
						break;
					case "B":
						ob.style = arr[i];
						break;
				}
			}
			return ob;

		}

		public static function converWordStyle(st : String , ob : Object) : String
		{

			var t : String = '<font ';
			if (ob.color != null)
			{
				t = t + 'color="' + ob.color + '" ';
			}
			if (ob.size != null)
			{
				t = t + 'size="' + ob.size + '" ';
			}

			if (ob.face != null)
			{
				t = t + 'face="' + ob.face + '" ';
			}
			if (ob.letterspacing != null)
			{
				t = t + 'letterspacing="' + ob.letterspacing + '" ';
			}



			if (ob.size != null || ob.color != null || ob.font != null || ob.letterspacing != null)
			{

				st = t + '>' + st + '</font>';

			}
			if (ob.style != null)
			{
				switch (ob.style)
				{
					case "i":
						st = '<i>' + st + '</i>';
						break;
					case "u":
						st = '<u>' + st + '</u>';
						break;
					case "b":
						st = '<b>' + st + '</b>';
						break;

				}
			}

			return st;
		}

		public static function changeStyle(txt : TextField , ob : Object) : void
		{
			var st : String = txt.text;
			var t : String = '<font ';
			if (ob.color != null)
			{
				t = t + 'color="' + ob.color + '" ';
			}
			if (ob.size != null)
			{
				t = t + 'size="' + ob.size + '" ';
			}

			if (ob.face != null)
			{
				t = t + 'face="' + ob.face + '" ';
			}
			if (ob.letterspacing != null)
			{
				t = t + 'letterspacing="' + ob.letterspacing + '" ';
			}



			if (ob.size != null || ob.color != null || ob.font != null || ob.letterspacing != null)
			{

				st = t + '>' + st + '</font>';

			}

			t = "<p ";
			if (ob.cssName != null)
			{
				t = t + 'class="' + ob.cssName + '" ';
			}

			if (ob.align != null)
			{
				t = t + 'align="' + ob.align + '" ';
			}
			t = t + '>';

			if (ob.cssName != null || ob.align != null)
			{
				st = t + st + '</p>';
			}

			switch (ob.style)
			{
				case "i":
					st = '<i>' + st + '</i>';
					break;
				case "u":
					st = '<u>' + st + '</u>';
					break;
				case "b":
					st = '<b>' + st + '</b>';
					break;

			}
			if (txt.filters.length == 0)
			{
				txt.filters = TextStyleUtil.filters;
			}
			txt.htmlText = st;
		}

		/**
		 *

		 * @param color 颜色
		 * @param size  大小
		 * @param align 排版   center left right
		 * @param letter 字体间距
		 * @param cssName
		 * @param style 样式  u 下划线  b 粗体 i 斜体
		 * @return
		 *
		 */
		public static function maskStyleObject(color : uint = 10 , size : int = 12 , font : Array = null , letter : int = 0 , align : String = "center" , cssName : String = "" , style : String = "") : Object
		{
			var ob : Object = new Object();

			var _font : String = "";
			if (color == 10)
			{
				color = TextStyleUtil.color;
			}
			if (font != null)
			{
				var len : int = font.length;
				for (var i : int = 0 ; i < len ; i++)
				{
					var st : String = font[i] as String;
					_font = _font + st + ",";
				}
				_font = _font.slice(0 , _font.length - 1);
				ob.face = _font;

			}
			ob.letterspacing = letter.toString();
			ob.color = "#" + color.toString(16);
			ob.size = size.toString();
			ob.align = align;
			if (cssName != "")
				ob.cssName = cssName;
			if (style != "")
				ob.style = style;
			return ob;
		}


	}
}